package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.query.PlatformRoleQuery;
import com.coffee.admin.api.platform.req.PlatformRoleListReq;
import com.coffee.admin.api.platform.req.PlatformRoleReq;
import com.coffee.admin.api.platform.result.PlatformPermissionResult;
import com.coffee.admin.api.platform.result.PlatformRoleResult;
import com.coffee.admin.api.platform.service.PlatformPermissionService;
import com.coffee.admin.api.platform.service.PlatformRoleService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/platform/role")
public class PlatformRoleController extends BaseController {

    @DubboReference(check = false)
    private PlatformRoleService platformRoleService;

    @DubboReference(check = false)
    private PlatformPermissionService platformPermissionService;

    @OperLog(operModul = "平台-系统设置-权限管理", operDesc = "查询角色列表")
    @PlatformLoginCheck(alias = "PlatformRoleListAlias")
    @RequestMapping(value = "/findList")
    public Result<Page<PlatformRoleResult>> findList(HttpServletRequest request, @RequestBody PlatformRoleListReq params) {
        Result<Page<PlatformRoleResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        result.setData(platformRoleService.getPager(new PlatformRoleQuery(), pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-系统设置-权限管理", operDesc = "删除角色")
    @PlatformLoginCheck(alias = "PlatformRoleOperationAlias")
    @RequestMapping(value = "/delete")
    public Result<String> deleteRole(@RequestBody JSONObject json) throws BusinessException {
        Result<String> result = Result.success(null);
        Integer id = json.getInteger("id");
        platformRoleService.delete(id);
        return result;
    }

    @OperLog(operModul = "平台-系统设置-权限管理", operDesc = "获取平台所有权限")
    @PlatformLoginCheck(alias = "PlatformRoleListAlias")
    @RequestMapping(value = "/permission/getAll")
    public Result<List<PlatformPermissionResult>> getAllPermission(HttpServletRequest request) {
        Result<List<PlatformPermissionResult>> result = Result.success(null);
        result.setData(platformPermissionService.getPermissionListByLoginMerchant());
        return result;
    }

    @OperLog(operModul = "平台-系统设置-权限管理", operDesc = "保存角色")
    @PlatformLoginCheck(alias = "PlatformRoleOperationAlias")
    @RequestMapping(value = "/save")
    public Result<String> save(@RequestBody PlatformRoleReq roleReq) throws BusinessException{
        Result<String> result = Result.success(null);
        platformRoleService.addOrUpdate(roleReq);
        return result;
    }

    @OperLog(operModul = "平台-系统设置-权限管理", operDesc = "修改角色")
    @PlatformLoginCheck(alias = "PlatformRoleOperationAlias")
    @RequestMapping(value = "/update")
    public Result<String> update(@RequestBody PlatformRoleReq roleReq) throws BusinessException{
        Result<String> result = Result.success(null);
        platformRoleService.addOrUpdate(roleReq);
        return result;
    }

}
